package sns.action;

import java.util.List;

import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import sns.bean.*;
import sns.dao.*;
import sns.service.*;
import sns.util.RecommendUser;

/**
 * This action is used to test all the methods realizes in the service package
 * 
 * @author caiyu
 * 
 */
public class TestForServiceAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private CommentDao commentDao;
	private ImpressionDao impressionDao;
	private MblogDao mblogDao;
	private RelationDao relationDao;
	private TagDao tagDao;
	private UserDao userDao;

	private CommentService commentService;
	private ImpressionService impressionService;
	private MblogService mblogService;
	private RelationService relationService;
	private TagService tagService;
	private UserService userService;
	private RecommendService recommendService;

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	public void setImpressionDao(ImpressionDao impressionDao) {
		this.impressionDao = impressionDao;
	}

	public void setMblogDao(MblogDao mblogDao) {
		this.mblogDao = mblogDao;
	}

	public void setRelationDao(RelationDao relationDao) {
		this.relationDao = relationDao;
	}

	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public void setImpressionService(ImpressionService impressionService) {
		this.impressionService = impressionService;
	}

	public void setMblogService(MblogService mblogService) {
		this.mblogService = mblogService;
	}

	public void setRelationService(RelationService relationService) {
		this.relationService = relationService;
	}

	public void setTagService(TagService tagService) {
		this.tagService = tagService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setRecommendService(RecommendService recommendService) {
		this.recommendService = recommendService;
	}

	@Transactional
	public String execute() {
		// TODO test for service
		System.out.println("===== Test for service: =====");
		testUserService();
		testRelationService();
		testTagService();
		testImpressionService();
		testMblogService();
		testCommentService();
		testRecommendService();
		
		return Action.SUCCESS;
	}

	private void testRecommendService() {
		User user = userService.findByUsername("user1");
		System.out.println("=== Test for RecommendService: ===");
		List<RecommendUser> rec_users = recommendService
				.recommendUsers(user, 5);
		printOut(rec_users);
	}

	private void testCommentService() {
		System.out.println("=== Test for CommentService: ===");
		testGetCommentById();
		testGetCommentOfMblog();
	}

	private void testGetCommentOfMblog() {
		List<Comment> comments;
		System.out.println("= Test getCommentOfMblog: =");
		comments = commentService.getCommentOfMblog(mblogService
				.getMblogById(0L));
		printOut(comments);
		comments = commentService.getCommentOfMblog(mblogService
				.getMblogById(1L));
		printOut(comments);
		comments = commentService.getCommentOfMblog(mblogService
				.getMblogById(2L));
		printOut(comments);
	}

	private void testGetCommentById() {
		Comment comment;
		System.out.println("= Test getCommentById: =");
		comment = commentService.getCommentById(0L);
		printOut(comment);
		comment = commentService.getCommentById(1L);
		printOut(comment);
	}

	private void testMblogService() {
		System.out.println("=== Test for MblogService: ===");
		testGetAllMblogs();
		testGetFolloweeMblogs();
		testGetMblogNum();
		testGetUserMblogs();
	}

	private void testGetUserMblogs() {
		List<Mblog> mblogs;
		System.out.println("= Test getUserMblogs: =");
		mblogs = mblogService
				.getUserMblogs(userService.findByUsername("user0"));
		printOut(mblogs);
		mblogs = mblogService
				.getUserMblogs(userService.findByUsername("user1"));
		printOut(mblogs);
		mblogs = mblogService
				.getUserMblogs(userService.findByUsername("user2"));
		printOut(mblogs);
		mblogs = mblogService
				.getUserMblogs(userService.findByUsername("user3"));
		printOut(mblogs);
	}

	private void testGetMblogNum() {
		int r;
		System.out.println("= Test getMblogNum: =");
		r = mblogService.getMblogNum(userService.findByUsername("user0"));
		System.out.println(r);
		r = mblogService.getMblogNum(userService.findByUsername("user1"));
		System.out.println(r);
		r = mblogService.getMblogNum(userService.findByUsername("user2"));
		System.out.println(r);
		r = mblogService.getMblogNum(userService.findByUsername("user3"));
		System.out.println(r);
	}

	private void testGetFolloweeMblogs() {
		List<Mblog> mblogs;
		System.out.println("= Test getFolloweeMblogs: =");
		mblogs = mblogService.getFolloweeMblogs(userService
				.findByUsername("user3"));
		printOut(mblogs);
		mblogs = mblogService.getFolloweeMblogs(userService
				.findByUsername("user2"));
		printOut(mblogs);
		mblogs = mblogService.getFolloweeMblogs(userService
				.findByUsername("user1"));
		printOut(mblogs);
	}

	private void testGetAllMblogs() {
		List<Mblog> mblogs;
		System.out.println("= Test getAllMblogs: =");
		mblogs = mblogService.getAllMblogs(2);
		printOut(mblogs);
		mblogs = mblogService.getAllMblogs(10);
		printOut(mblogs);
	}

	private void testImpressionService() {
		System.out.println("=== Test for ImpressionService: ===");
		testGetImpressionById();
		testGetImpressionsOfUser();
	}

	private void testGetImpressionsOfUser() {
		List<Impression> impressions;
		System.out.println("= Test getImpressionsOfUser: =");
		impressions = impressionService.getImpressionsOfUser(userService
				.findByUsername("user1"));
		printOut(impressions);
		impressions = impressionService.getImpressionsOfUser(userService
				.findByUsername("user2"));
		printOut(impressions);
	}

	private void testGetImpressionById() {
		Impression impression;
		System.out.println("= Test getImpressionById: =");
		impression = impressionService.getImpressionById(0L);
		printOut(impression);
		impression = impressionService.getImpressionById(1L);
		printOut(impression);
	}

	private void testTagService() {
		System.out.println("=== Test for TagService: ===");
		testGetTagById();
		testGetTagsOfUser();
	}

	private void testGetTagsOfUser() {
		List<Tag> tags;
		System.out.println("= Test getTagsOfUser: =");
		tags = tagService.getTagsOfUser(userService.findByUsername("user1"));
		printOut(tags);
		tags = tagService.getTagsOfUser(userService.findByUsername("user2"));
		printOut(tags);
		tags = tagService.getTagsOfUser(userService.findByUsername("user3"));
		printOut(tags);
	}

	private void testGetTagById() {
		Tag tag;
		System.out.println("= Test getTagById: =");
		tag = tagService.getTagById(0L);
		printOut(tag);
		tag = tagService.getTagById(1L);
		printOut(tag);
		tag = tagService.getTagById(2L);
		printOut(tag);
	}

	private void testRelationService() {
		System.out.println("=== Test for RelationService: ===");
		testCheckIfFollow();
		testGetFolloweeNum();
		testGetFollowerNum();
		testGetRelation();
	}

	private void testGetRelation() {
		Relation relation;
		System.out.println("= Test getRelation: =");
		relation = relationService.getRelation(
				userService.findByUsername("user1"),
				userService.findByUsername("user2"));
		printOut(relation);
		relation = relationService.getRelation(
				userService.findByUsername("user2"),
				userService.findByUsername("user1"));
		printOut(relation);
	}

	private void testGetFollowerNum() {
		int r;
		System.out.println("= Test getFollowerNum: =");
		r = relationService.getFollowerNum(userService.findByUsername("user1"));
		System.out.println(r);
		r = relationService.getFollowerNum(userService.findByUsername("user2"));
		System.out.println(r);
		r = relationService.getFollowerNum(userService.findByUsername("user3"));
		System.out.println(r);
	}

	private void testGetFolloweeNum() {
		int r;
		System.out.println("= Test getFolloweeNum: =");
		r = relationService.getFolloweeNum(userService.findByUsername("user1"));
		System.out.println(r);
		r = relationService.getFolloweeNum(userService.findByUsername("user2"));
		System.out.println(r);
		r = relationService.getFolloweeNum(userService.findByUsername("user3"));
		System.out.println(r);
	}

	private void testCheckIfFollow() {
		boolean r;
		System.out.println("= Test checkIfFollow: =");
		r = relationService.checkIfFollow(userService.findByUsername("user1"),
				userService.findByUsername("user2"));
		System.out.println(r);
		r = relationService.checkIfFollow(userService.findByUsername("user2"),
				userService.findByUsername("user1"));
		System.out.println(r);
	}

	private void testUserService() {
		System.out.println("=== Test for UserService: ===");
		testFindByUsername();
		testFindUserById();
		testGetCurrentUser();
		testGetFollowees();
		testGetFollowers();
		testGetHotUsers();
		testGetNewUsers();
		testQueryUser();
	}

	private void testQueryUser() {
		List<User> users;
		System.out.println("= Test queryUser: =");
		users = userService.queryUser("user");
		printOut(users);
		users = userService.queryUser("1");
		printOut(users);
	}

	private void testGetNewUsers() {
		List<User> users;
		System.out.println("= Test getNewUsers: =");
		users = userService.getNewUsers(2);
		printOut(users);
		users = userService.getNewUsers(10);
		printOut(users);
	}

	private void testGetHotUsers() {
		List<User> users;
		System.out.println("= Test getHotUsers: =");
		users = userService.getHotUsers(2);
		printOut(users);
		users = userService.getHotUsers(10);
		printOut(users);
	}

	private void testGetFollowers() {
		List<User> users;
		System.out.println("= Test getFollowers: =");
		users = userService.getFollowers(userService.findByUsername("user0"));
		printOut(users);
		users = userService.getFollowers(userService.findByUsername("user1"));
		printOut(users);
		users = userService.getFollowers(userService.findByUsername("user2"));
		printOut(users);
		users = userService.getFollowers(userService.findByUsername("user3"));
		printOut(users);
		users = userService.getFollowers(userService.findByUsername("user4"));
		printOut(users);
	}

	private void testGetFollowees() {
		List<User> users;
		System.out.println("= Test getFollowees: =");
		users = userService.getFollowees(userService.findByUsername("user0"));
		printOut(users);
		users = userService.getFollowees(userService.findByUsername("user1"));
		printOut(users);
		users = userService.getFollowees(userService.findByUsername("user2"));
		printOut(users);
		users = userService.getFollowees(userService.findByUsername("user3"));
		printOut(users);
		users = userService.getFollowees(userService.findByUsername("user4"));
		printOut(users);
	}

	private void testGetCurrentUser() {
		User user;
		System.out.println("= Test getCurrentUser: =");
		user = userService.getCurrentUser();
		printOut(user);
	}

	private void testFindUserById() {
		User user;
		System.out.println("= Test findUserById: =");
		user = userService.findUserById(0L);
		printOut(user);
		user = userService.findUserById(1L);
		printOut(user);
		user = userService.findUserById(2L);
		printOut(user);
		user = userService.findUserById(3L);
		printOut(user);
		user = userService.findUserById(4L);
		printOut(user);
	}

	private void testFindByUsername() {
		User user;
		System.out.println("= Test findByUsername: =");
		user = userService.findByUsername("user0");
		printOut(user);
		user = userService.findByUsername("user1");
		printOut(user);
		user = userService.findByUsername("user2");
		printOut(user);
		user = userService.findByUsername("user3");
		printOut(user);
		user = userService.findByUsername("user4");
		printOut(user);
	}

	private void printOut(List os) {
		System.out.println("List begin");
		if (os == null) {
			System.out.println("Objects is NULL");
		} else {
			for (Object o : os) {
				printOut(o);
			}
		}
		System.out.println("List end");
	}

	private void printOut(Object o) {
		if (o == null) {
			System.out.println("Object is NULL");
		} else {
			System.out.println(o.toString());
		}
	}

}
